<template>
	<view class="content">
		<!-- <view class="navbar">
			<view 
				v-for="(item, index) in navList" :key="index" 
				class="nav-item nav-item-btn" 
				:class="{current: tabCurrentIndex === index, nav_item_reddot: item.reddot}"
				@click="tabClick(index)"
			>
				{{item.text}}
			</view>
		</view> -->

		<swiper :current="tabCurrentIndex" class="swiper-box" duration="300" @change="changeTab">
			<swiper-item class="tab-content" v-for="(tabItem,tabIndex) in navList" :key="tabIndex">
				<scroll-view 
					class="list-scroll-content" 
					scroll-y
					@scrolltolower="loadData"
				>
					<!-- 空白页 -->
					<empty v-if="tabItem.loaded === true && tabItem.brocastList.length === 0"></empty>
					
					<!-- 信息列表 -->
					<view 
						v-for="(item,index) in tabItem.brocastList" :key="index"
						class="news-item" @click="readNews(item.id, tabIndex, index)"
					>
						<text class="time">{{item.time}}</text>
						<view class="news-content" @click="navToDetailPage(item.id, tabIndex, index)">
							<view class="img-wrapper" v-if="item.pic">
								<image class="pic" :src="item.pic"></image>
								<view class="cover" v-if="item.type == 2">
									{{i18n.end_act_title}}
								</view>
							</view>
							<text class="title" >{{item.title}}</text>
							<text class="introduce">
								{{item.introduce}}
							</text>
						</view>
					</view>
					 
					<uni-load-more :status="tabItem.loadingType"></uni-load-more>
					
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template> 

<script>
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	import request_async from '../../common/request_async.js';
	import empty from "@/components/empty";
	import { dateUtils } from '@/common/util';
	import { inArray } from '@/common/common';
	export default {
		components: {
			uniLoadMore,
			empty
		},
		data() {
			return {
				readedIds_key : '',
				readedIds_value : [],
				tabCurrentIndex: 0,
				navList: [
					{
						type: 0,
						reddot : false,
						text: this.$lang.news_news.type0_title,
						loadingType: 'more',
						brocastList: []
					}
				],
				
			};
		},
		computed: {
			i18n() {
				return this.$lang.news_news
			},
		},
		onShow() {
			uni.setNavigationBarTitle({
			    title: this.$lang.news_news.title
			});
		},
		onLoad(options){
			
			this.tabCurrentIndex = options.type ? options.type : 0;
			// #ifndef MP
			this.loadData()
			// #endif
			// #ifdef MP
			if(options.type == 0){
				this.loadData()
			}
			// #endif
			this.loadAll();
		},
		 
		methods: {
			loadAll(){
				//获取已读id数组
				var pages = getCurrentPages();
				var route = (pages[pages.length - 1]).route;
				// console.log(route)
				this.readedIds_key = route+'_readed_ids';
				// uni.setStorageSync(this.readedIds_key, []);
				try {
				    this.readedIds_value = uni.getStorageSync(this.readedIds_key);
					console.log('onload '+this.readedIds_key, this.readedIds_value)
				} catch (e) {
				    // error
				}
				if(!this.readedIds_value){
					this.readedIds_value = [];
				}
				var isAllReddot = false;
				for(var index = 0; index < this.navList.length; index++){
					var oneRed = this.loadData('', index);
					if(oneRed){
						isAllReddot = true;
					}
				}
				this.$store.state.newNews = isAllReddot;
			},
			//获取信息列表
			async loadData(source, nowIndex){
				//这里是将信息挂载到tab列表下
				let index = nowIndex !== undefined ? nowIndex : this.tabCurrentIndex;
				console.log('loadData index : '+index)
				let navItem = this.navList[index];
				let type = navItem.type;
				let page = navItem.page ? navItem.page  : 1;
				
				if(navItem.loaded === true){
					//tab切换只有第一次需要加载数据
					return;
				}
				if(navItem.loadingType === 'loading'){
					//防止重复加载
					return;
				}
				
				navItem.loadingType = 'loading';
				
				var res = await request_async({
					name: 'news.list',
					method: 'POST',
					data: {page : page, type : type}
				})
				if(res.code != 0){
					return false;
				}
				var pageInfo = res.data;
				let brocastList = pageInfo.data.filter(item=>{
					//添加不同状态下信息的表现形式
					item = Object.assign(item, this.newsStateExp(item));
					//演示数据所以自己进行状态筛选
					return item.type === type
				});
				
				var isReddot = false;
				brocastList.forEach(item=>{
					navItem.brocastList.push(item);
					if(item.reddot){
						this.navList[index].reddot = true;
						isReddot = true;
					}
				})
				
				//判断是否还有数据， 有改为 more， 没有改为noMore 
				if(pageInfo.last_page > page){
					navItem.page = page+1;
					navItem.loadingType = 'more';
				}else{
					navItem.loadingType = 'noMore';
					//loaded新字段用于表示数据加载完毕，如果为空可以显示空白页
					this.$set(navItem, 'loaded', true);
				}
				return isReddot;
			}, 
			navToDetailPage(id, tabIndex, index) {
				this.readNews(id, tabIndex, index);
				uni.navigateTo({
					url: `/pages/news/detail?id=${id}`
				})
			},
			//swiper 切换
			changeTab(e){
				this.tabCurrentIndex = e.target.current;
				this.loadData('tabChange');
			},
			//顶部tab点击
			tabClick(index){
				this.tabCurrentIndex = index;
			},
			readNews(id, tabIndex, index){
				if(this.$store.state.hasLogin){
					console.log('read')
					this.$request({
						name: 'news.readed',
						method: 'POST',
						data: {id : id},
						success : function(){}
					})
				}
				var item = this.navList[this.tabCurrentIndex].brocastList[index];
				if(item.reddot == false){
					return false;
				}
				var id = item.id;
				var nowIds = this.readedIds_value;
				console.log('read id '+id, 'old nowIds '+this.readedIds_value)
				if(!inArray(id, nowIds)){
					nowIds.push(id);
					this.readedIds_value = nowIds;
					try {
						console.log('set '+this.readedIds_key, this.readedIds_value);
					    uni.setStorageSync(this.readedIds_key, this.readedIds_value);
					} catch (e) {
					    // error
					}
				}
				this.navList[this.tabCurrentIndex].brocastList[index].reddot = false;
				this.freshReddot();
			},
			//删除信息
			deleteNews(index){
				uni.showLoading({
					title: this.$lang.news_news.waitfor_title
				})
				setTimeout(()=>{
					this.navList[this.tabCurrentIndex].brocastList.splice(index, 1);
					uni.hideLoading();
				}, 600)
				this.freshReddot();
			},
			//刷新红点
			freshReddot(){
				for(var index = 0; index < this.navList.length; index++){
					this.navList[index].reddot = false;
					for(var index2 in this.navList[index].brocastList){
						if(this.navList[index].brocastList[index2].reddot){
							this.navList[index].reddot = true;
						}
					}
				}
			},

			//信息状态文字和颜色
			newsStateExp(item){
				var type = item.type;
				var id = item.id;
				let stateTip = '',
					stateTipColor = '#fa436a';
				switch(+type){
					case 1:
						stateTip = '待付款'; break;
					case 2:
						stateTip = '待发货'; break;
					case 9:
						stateTip = '信息已关闭'; 
						stateTipColor = '#909399';
						break;
					//更多自定义
				}
				//标志已读
				let reddot = true;
				
				if(inArray(id, this.readedIds_value)){
					
					reddot = false;
				}
				
				if(reddot == true && item.readed){
					//未读
					reddot = false;
				}
				console.log(id, this.readedIds_value, reddot, item.readed, reddot)
				let time = item.created_at;
				if(item.created_at){
					time = dateUtils.humanize(item.created_at);
				}
				return {stateTip, stateTipColor, reddot, time};
			}
		},
	}
</script>

<style lang="scss">
	page, .content{
		background: $page-color-base;
		height: 100%;
	}
	
	.swiper-box{
		height: calc(100% - 40px);
	}
	.list-scroll-content{
		height: 100%;
	}
	.time {
			display: flex;
			align-items: center;
			justify-content: center;
			height: 80upx;
			padding-top: 10upx;
			font-size: 26upx;
			color: #7d7d7d;
	}
	.navbar{
		display: flex;
		height: 40px;
		padding: 0 5px;
		background: #fff;
		box-shadow: 0 1px 5px rgba(0,0,0,.06);
		position: relative;
		z-index: 10;
		.nav-item{
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			font-size: 15px;
			color: $font-color-dark;
			position: relative;
			&.current{
				color: $base-color;
				&:after{
					content: '';
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translateX(-50%);
					width: 44px;
					height: 0;
					border-bottom: 2px solid $base-color;
				}
			}
		}
	}
	.nav-item-btn {
		position: relative;
		width: auto;
		margin: 0 2px;
		word-break: keep-all;
		white-space: pre;
		cursor: pointer;
	}
	.nav_item_reddot:after {
		content: attr(badge-text);
		// position: absolute;
		background-color: red;
		color: #fff;
		width: 18px;
		height: 18px;
		line-height: 18px;
		border-radius: 18px;
		overflow: hidden;
		-webkit-transform: scale(.5) translate(40%,-40%);
		transform: scale(.5) translate(40%,-40%);
		-webkit-transform-origin: 100% 0;
		transform-origin: 100% 0;
	}
	.title_reddot:after {
		content: attr(badge-text);
		background-color: red;
		color: #fff;
		width: 18px;
		height: 18px;
		float: right;
		line-height: 18px;
		border-radius: 18px;
		overflow: hidden;
		-webkit-transform: scale(.5) translate(40%,-40%);
		transform: scale(.5) translate(40%,-40%);
		-webkit-transform-origin: 100% 0;
		transform-origin: 100% 0;
	}
	.uni-swiper-item{
		height: auto;
	}
	.news-item{
		display: flex;
		flex-direction: column;
		padding-left: 30upx;
		background: #fff;
		margin-top: 16upx;
		.news-content {
			width: 710upx;
			padding: 0 24upx;
			background-color: #fff;
			border-radius: 4upx;
		}
		.title {
			display: flex;
			align-items: center;
			height: 90upx;
			font-size: 32upx;
			color: #303133;
		}
		.img-wrapper {
			width: 100%;
			height: 260upx;
			position: relative;
		}
		.pic {
			display: block;
			width: 100%;
			height: 100%;
			border-radius: 6upx;
		}
		.cover {
			display: flex;
			justify-content: center;
			align-items: center;
			position: absolute;
			left: 0;
			top: 0;
			width: 100%;
			height: 100%;
			background-color: rgba(0, 0, 0, .5);
			font-size: 36upx;
			color: #fff;
		}
		.introduce {
			display: inline-block;
			padding: 16upx 0;
			font-size: 28upx;
			color: #d0d0d0;
			line-height: 38upx;
		}
		.bot {
			display: flex;
			align-items: center;
			justify-content: space-between;
			height: 90upx;
			font-size: 24upx;
			color: #707070;
			position: relative;
		}
		.more-icon {
			font-size: 32upx;
		}
	}
	
	
	/* load-more */
	.uni-load-more {
		display: flex;
		flex-direction: row;
		height: 80upx;
		align-items: center;
		justify-content: center
	}
	
	.uni-load-more__text {
		font-size: 28upx;
		color: #999
	}
	
	.uni-load-more__img {
		height: 24px;
		width: 24px;
		margin-right: 10px
	}
	
	.uni-load-more__img>view {
		position: absolute
	}
	
	.uni-load-more__img>view view {
		width: 6px;
		height: 2px;
		border-top-left-radius: 1px;
		border-bottom-left-radius: 1px;
		background: #999;
		position: absolute;
		opacity: .2;
		transform-origin: 50%;
		animation: load 1.56s ease infinite
	}
	
	.uni-load-more__img>view view:nth-child(1) {
		transform: rotate(90deg);
		top: 2px;
		left: 9px
	}
	
	.uni-load-more__img>view view:nth-child(2) {
		transform: rotate(180deg);
		top: 11px;
		right: 0
	}
	
	.uni-load-more__img>view view:nth-child(3) {
		transform: rotate(270deg);
		bottom: 2px;
		left: 9px
	}
	
	.uni-load-more__img>view view:nth-child(4) {
		top: 11px;
		left: 0
	}
	
	.load1,
	.load2,
	.load3 {
		height: 24px;
		width: 24px
	}
	
	.load2 {
		transform: rotate(30deg)
	}
	
	.load3 {
		transform: rotate(60deg)
	}
	
	.load1 view:nth-child(1) {
		animation-delay: 0s
	}
	
	.load2 view:nth-child(1) {
		animation-delay: .13s
	}
	
	.load3 view:nth-child(1) {
		animation-delay: .26s
	}
	
	.load1 view:nth-child(2) {
		animation-delay: .39s
	}
	
	.load2 view:nth-child(2) {
		animation-delay: .52s
	}
	
	.load3 view:nth-child(2) {
		animation-delay: .65s
	}
	
	.load1 view:nth-child(3) {
		animation-delay: .78s
	}
	
	.load2 view:nth-child(3) {
		animation-delay: .91s
	}
	
	.load3 view:nth-child(3) {
		animation-delay: 1.04s
	}
	
	.load1 view:nth-child(4) {
		animation-delay: 1.17s
	}
	
	.load2 view:nth-child(4) {
		animation-delay: 1.3s
	}
	
	.load3 view:nth-child(4) {
		animation-delay: 1.43s
	}
	
	@-webkit-keyframes load {
		0% {
			opacity: 1
		}
	
		100% {
			opacity: .2
		}
	}
</style>
